package org.bonorsoft.modules.education.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.bonorsoft.common.api.vo.Result;
import org.bonorsoft.common.system.query.QueryGenerator;
import org.bonorsoft.common.util.oConvertUtils;
import org.bonorsoft.modules.education.entity.StudentNumber;
import org.bonorsoft.modules.education.service.IStudentNumberService;
import java.util.Date;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.bonorsoft.common.aspect.annotation.AutoLog;
import org.bonorsoft.common.system.base.controller.BonorController;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.apache.shiro.authz.annotation.RequiresPermissions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

 /**
 * @Description: 每年各县区学生数量
 * @Author: WilliamYe
 * @Date:   2020-11-26
 * @Version: V1.0
 */
@Slf4j
@Controller
@RequestMapping("/education/number")
public class StudentNumberController extends BonorController<StudentNumber, IStudentNumberService> {
    private String prefix = "education/number";

	@Autowired
	private IStudentNumberService studentNumberService;

	/**
	*首页
	*/
	@RequiresPermissions("education:number:view")
    @GetMapping()
    public String index()
    {
     return prefix + "/number";
    }

	/**
	 * 分页列表查询
	 *
	 * @param studentNumber
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "每年各县区学生数量-分页列表查询")
	@RequiresPermissions("education:number:list")
	@GetMapping(value = "/list")
	@ResponseBody
	public Result<?> queryPageList(StudentNumber studentNumber,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<StudentNumber> queryWrapper = QueryGenerator.initQueryWrapper(studentNumber, req.getParameterMap());
		Page<StudentNumber> page = new Page<StudentNumber>(pageNo, pageSize);
		IPage<StudentNumber> pageList = studentNumberService.page(page, queryWrapper);
		return Result.ok(pageList);
	}

    /**
    * 添加页面
    *
    */
    @GetMapping("/add")
    public String add()
    {
     return prefix + "/add";
    }

	/**
	 * 添加
	 *
	 * @param studentNumber
	 * @return
	 */
	@AutoLog(value = "每年各县区学生数量-添加")
	@RequiresPermissions("education:number:add")
	@PostMapping(value = "/add")
    @ResponseBody
	public Result<?> add(@Validated StudentNumber studentNumber) {
		studentNumberService.save(studentNumber);
		return Result.ok("添加成功！");
	}

    /**
    * 编辑页面
    */
	@GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map)
    {
     map.put("editInfo", studentNumberService.getById(id));
     return prefix + "/edit";
    }

	/**
	 * 编辑
	 *
	 * @param studentNumber
	 * @return
	 */
	@AutoLog(value = "每年各县区学生数量-编辑")
	@RequiresPermissions("education:number:edit")
	@PostMapping(value = "/edit")
	@ResponseBody
	public Result<?> edit(@Validated StudentNumber studentNumber) {
		studentNumberService.updateById(studentNumber);
		return Result.ok("编辑成功!");
	}

	/**
	 * 通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "每年各县区学生数量-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		studentNumberService.removeById(id);
		return Result.ok("删除成功!");
	}

	/**
	 * 批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "每年各县区学生数量-批量删除")
	@RequiresPermissions("education:number:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	@ResponseBody
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.studentNumberService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.ok("批量删除成功！");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "每年各县区学生数量-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		StudentNumber studentNumber = studentNumberService.getById(id);
		return Result.ok(studentNumber);
	}

  /**
   * 导出excel
   *
   * @param request
   * @param studentNumber
   */
  @AutoLog(value = "每年各县区学生数量-导出excel")
  @RequestMapping(value = "/exportXls")
  public ModelAndView exportXls(HttpServletRequest request, StudentNumber studentNumber) {
      return super.exportXls(request, studentNumber, StudentNumber.class, "每年各县区学生数量");
  }

	 /**
	  * 根据模板自定义导出下载模板
	  * @return
	  */
	 @AutoLog(value = "每年各县区学生数量-导出下载模板", operateType = 6)
	 @RequestMapping(value = "/importTemplate")
	 public ModelAndView importTemplate() {
		 return super.importTemplateXls(StudentNumber.class ,"每年各县区学生数量");
	 }

  /**
   * 通过excel导入数据
   *
   * @param request
   * @param response
   * @return
   */
  @AutoLog(value = "每年各县区学生数量-导入数据")
  @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
  @ResponseBody
  public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
      return super.importExcel(request, response, StudentNumber.class);
  }

}
